using MicroMsg.Common.Event;
using MicroMsg.Common.Timer;
using MicroMsg.Common.Utils;
using MicroMsg.Network;
using System;
namespace MicroMsg.Plugin.Sns.Scene
{
	public class DownloadMediaService
	{
		private const string TAG = "DownloadMediaService";
		private static TimerObject mTimerObject;
		public static bool downloadSnsInfo(snsMediaDownloadInfo mediaInfo)
		{
			if (mediaInfo == null || string.IsNullOrEmpty(mediaInfo.string_0))
			{
				Log.smethod_1("DownloadMediaService", "Invalid input parameters");
				return false;
			}
			if (DownloadMediaContextMgr.Instance.isExistInQueue(mediaInfo.snsObjID, mediaInfo.string_0))
			{
				return true;
			}
			mediaInfo.startPos = 0;
			mediaInfo.totalLength = 0;
			mediaInfo.status = 0;
			mediaInfo.retryTimes = 0;
			if (mediaInfo.priority == SnsPriority.LOW)
			{
				DownloadMediaContextMgr.Instance.putToTail(new DownloadMediaContext(mediaInfo));
			}
			else
			{
				if (mediaInfo.priority == SnsPriority.HIGH)
				{
					DownloadMediaContextMgr.Instance.putToHead(new DownloadMediaContext(mediaInfo));
				}
			}
			DownloadMediaService.checkReadyContextDispatcher();
			return true;
		}
		private static void checkReadyContextDispatcher()
		{
			if (DownloadMediaService.mTimerObject == null)
			{
				Log.smethod_5("DownloadMediaService", "download service dispatcher startup. ");
				DownloadMediaService.mTimerObject = TimerService.addTimer(2, new EventHandler(DownloadMediaService.onContextDispatcher), 1, -1);
				DownloadMediaService.mTimerObject.start();
			}
			DownloadMediaService.onContextDispatcher(null, null);
		}
		public static void onContextDispatcher(object sender, EventArgs e)
		{
			DownloadMediaContextMgr.Instance.clearnFinishedContext();
			if (DownloadMediaContextMgr.Instance.getCount() <= 0)
			{
				Log.smethod_4("DownloadMediaService", "all ready, close dispatcher timer. ");
				if (DownloadMediaService.mTimerObject != null)
				{
					DownloadMediaService.mTimerObject.stop();
					DownloadMediaService.mTimerObject = null;
				}
				return;
			}
			int num = DownloadMediaContextMgr.Instance.countRunningContext();
			int sendPackNum = (int)DownloadMediaService.getSendPackNum();
			if (num >= sendPackNum)
			{
				Log.smethod_4("DownloadMediaService", "return request, because reach MaxRunningNum = " + sendPackNum);
				return;
			}
			for (int i = 0; i < sendPackNum - num; i++)
			{
				DownloadMediaContext firstContextNeedHandle = DownloadMediaContextMgr.Instance.getFirstContextNeedHandle();
				if (firstContextNeedHandle == null)
				{
					return;
				}
				if (firstContextNeedHandle.mSnsMediaInfo.urlType != SnsUrlType.MMSNS_URL_CDN && firstContextNeedHandle.mSnsMediaInfo.urlType != SnsUrlType.MMSNS_URL_OTHER)
				{
					if (firstContextNeedHandle.mSnsMediaInfo.urlType == SnsUrlType.MMSNS_URL_WEIXIN)
					{
						DebugEx.debugBreak();
						Log.smethod_5("DownloadMediaService", string.Concat(new object[]
						{
							"not support urlType now , urlType = ",
							firstContextNeedHandle.mSnsMediaInfo.urlType,
							" url = ",
							firstContextNeedHandle.mSnsMediaInfo.string_0
						}));
						firstContextNeedHandle.mSnsMediaInfo.retryTimes = 2;
						firstContextNeedHandle.mSnsMediaInfo.status = 4;
						firstContextNeedHandle.mSnsMediaInfo.object_0 = null;
						EventCenter.postEvent(EventConst.ON_MGR_SNS_GET_MEDIA_FINISH, firstContextNeedHandle.mSnsMediaInfo, null);
					}
					else
					{
						DebugEx.debugBreak();
						Log.smethod_1("DownloadMediaService", string.Concat(new object[]
						{
							"other urlType , urlType = ",
							firstContextNeedHandle.mSnsMediaInfo.urlType,
							" url = ",
							firstContextNeedHandle.mSnsMediaInfo.string_0
						}));
						firstContextNeedHandle.mSnsMediaInfo.retryTimes = 2;
						firstContextNeedHandle.mSnsMediaInfo.status = 4;
						firstContextNeedHandle.mSnsMediaInfo.object_0 = null;
						EventCenter.postEvent(EventConst.ON_MGR_SNS_GET_MEDIA_FINISH, firstContextNeedHandle.mSnsMediaInfo, null);
					}
				}
				else
				{
					NetSceneHttpDownloadService.getRequest(firstContextNeedHandle);
				}
			}
		}
		public static SendPackNum getSendPackNum()
		{
			if (NetworkDeviceWatcher.isCurrentWifi())
			{
				Log.smethod_4("DownloadMediaService", "Current net is Wifi");
				return SendPackNum.NETWORK_WIFI_NUM;
			}
			if (NetworkDeviceWatcher.isCurrentCell3G())
			{
				Log.smethod_4("DownloadMediaService", "Current net is 3G");
				return SendPackNum.NETWORK_3G_NUM;
			}
			if (NetworkDeviceWatcher.isCurrentCell2G())
			{
				Log.smethod_4("DownloadMediaService", "Current net is 2G");
				return SendPackNum.NETWORK_2G_NUM;
			}
			Log.smethod_4("DownloadMediaService", "Current net is UnKnow");
			return SendPackNum.NETWORK_3G_NUM;
		}
	}
}
